﻿<%@ WebHandler Language="C#" Class="UpdateElement" %>

using System;
using System.Web;
using System.Linq;
using System.Text.RegularExpressions;
public class UpdateElement : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        try
        {
            string parameter = context.Request.QueryString["para"];
            string value = context.Request.QueryString["value"];
            string username = context.Request.QueryString["key"];
            bool flag = false;
            int result = 0;
            if (parameter != null && value != null && username != null)
            {
                using (Up1VNDataContext dc = new Up1VNDataContext())
                {
                    User U = dc.Users.Single(u => u.Users_Name == username);
                    switch (parameter)
                    {
                        case "Email":
                            if (IsEmail(value))
                            {
                                U.Users_Email = value;
                                flag = true;
                                result = 1;
                            }
                            break;
                        case "Homephone":
                            if (CheckPhoneNumer(value))
                            {
                                U.Users_HomePhone = value;
                                flag = true;
                                result = 1;
                            }
                            break;
                        case "Mobile":
                            if (CheckPhoneNumer(value))
                            {
                                U.Users_MobilePhone = value;
                                flag = true;
                                result = 1;
                            }
                            break;
                        case "Yahoo":
                            U.Users_YahooNick = value;
                            flag = true;
                            result = 1;
                            break;
                        case "Skype":
                            U.Users_SkypeNick = value;
                            flag = true;
                            result = 1;
                            break;
                    }
                    if(flag)
                        dc.SubmitChanges();
                }
            }
            else
                result = 1;
            context.Response.Write(result);
        }
        catch (Exception)
        {
            context.Response.Write(0);
        }
    }
    public bool CheckPhoneNumer(string PhoneNumber)
    {
        try
        {
            string regex = @"^([0]{1}[1-9]{1})((\d{9})|(\d{8}))$";
            Regex reg = new Regex(regex);
            if(reg.IsMatch(PhoneNumber))
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }
    }

    public bool IsEmail(string Email)
    {
        try
        {
            string str = @"^([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6})$";
            Match math = Regex.Match(Email.Trim(), str, RegexOptions.IgnoreCase);
            if(math.Success)
                return true;
            else
                return false;
        }
        catch
        {
            return false;
        }
    }
    public bool IsReusable {
        get {
            return false;
        }
    }

}